package pw.hais.braintwists.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.squareup.okhttp.Response;

import java.util.List;

import pw.hais.braintwists.R;
import pw.hais.braintwists.app.BaseActivity;
import pw.hais.braintwists.dao.Tools2345Dao;
import pw.hais.braintwists.enums.ArticleType;
import pw.hais.braintwists.model.Article;
import pw.hais.http.base.OnHttpListener;
import pw.hais.utils.DownTime;
import pw.hais.utils.L;
import pw.hais.utils.SPUtils;

/**
 * 首页
 * Created by Hais1992 on 2015/12/11.
 */
public class MainActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private RadioGroup radiogrop_type;
    private TextView text_problem, text_answer, text_time;
    private TextView text_cg, text_username, text_jf;
    private Button btn_next, btn_show;
    private List<Article> list;

    private int now_index = 0;          //当前 index
    private int page = 1;               //当前页码
    private ArticleType articleType = ArticleType.Classic;    //当前类型

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radiogrop_type = (RadioGroup) findViewById(R.id.radiogrop_type);
        text_problem = (TextView) findViewById(R.id.text_problem);
        text_answer = (TextView) findViewById(R.id.text_answer);
        text_cg = (TextView) findViewById(R.id.text_cg);
        text_username = (TextView) findViewById(R.id.text_username);
        text_jf = (TextView) findViewById(R.id.text_jf);
        text_time = (TextView) findViewById(R.id.text_time);
        btn_next = (Button) findViewById(R.id.btn_next);
        btn_show = (Button) findViewById(R.id.btn_show);

        btn_next.setOnClickListener(this);
        btn_show.setOnClickListener(this);
        text_username.setOnClickListener(this);
        radiogrop_type.setOnCheckedChangeListener(this);

        Integer cs_number = SPUtils.getObject("braintwists.cs_number", Integer.class, 0);
        text_jf.setText("已超时：" + cs_number);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getHttpData();

    }


    private void getHttpData() {
        loadDialogShow("数据加载中.....");
        Tools2345Dao.getBrainTwists(articleType, page, new OnHttpListener<List<Article>>() {
            @Override
            public void onSuccess(Response response, List<Article> data) {
                if (list == null) list = data;
                else list.addAll(data);
                loadDialogDismiss();

                showDataToView();
            }
        });
    }

    private void showDataToView() {
        if (now_index >= list.size()) {
            page = page + 1;
            getHttpData();
        } else {
            if (list != null) {
                startTime();
                text_answer.setVisibility(View.GONE);

                Integer number = SPUtils.getObject("braintwists.number", Integer.class, 0);
                number = number + 1;
                text_cg.setText("已看题：" + number);
                SPUtils.saveObject("braintwists.number", number);

                text_problem.setText("" + list.get(now_index).problem);
                text_answer.setText("" + list.get(now_index).answer);
            }
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                now_index = now_index + 1;
                showDataToView();
                btn_show.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_show:
                text_answer.setVisibility(View.VISIBLE);
                btn_show.setVisibility(View.GONE);
                break;
            case R.id.text_username:
//                L.showShort("功能开发中...");
                if (app.user == null) {
                    startActivity(new Intent(context, LoginActivity.class));
                } else {
                    startActivity(new Intent(context, UserActivity.class));
                }
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radio_Classic:
                articleType = ArticleType.Classic;
                break;
            case R.id.radio_Clown:
                articleType = ArticleType.Clown;
                break;
            case R.id.radio_Adult:
                articleType = ArticleType.Adult;
                break;
            case R.id.radio_Children:
                articleType = ArticleType.Children;
                break;
            case R.id.radio_Maths:
                articleType = ArticleType.Maths;
                break;
            case R.id.radio_GiveSB:
                articleType = ArticleType.GiveSB;
                break;
            case R.id.radio_ColdJest:
                articleType = ArticleType.ColdJest;
                break;
            case R.id.radio_Feature:
                articleType = ArticleType.Feature;
                break;
        }
        list = null;
        page = 1;
        if (downTime != null) downTime.cancel();
        getHttpData();
    }

    private DownTime downTime;

    private void startTime() {
        //text_time
        if (downTime == null) {
            downTime = new DownTime(60 * 1000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    text_time.setText("" + (int) (millisUntilFinished / 1000));
                }

                @Override
                public void onFinish() {
                    btn_show.performClick();

                    Integer cs_number = SPUtils.getObject("braintwists.cs_number", Integer.class, 0);
                    cs_number = cs_number + 1;
                    text_jf.setText("已超时：" + cs_number);
                    SPUtils.saveObject("braintwists.cs_number", cs_number);
                }
            };
        } else {
            downTime.cancel();
        }
        downTime.start();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
//            Uri  uri = Uri.parse("http://hais.pw");
//            startActivity(new  Intent(Intent.ACTION_VIEW, uri));
            startActivity(new Intent(context, AuthorActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private long exitTime = 0;

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            L.showShort("再次按返回键退出 脑筋急转弯");
            exitTime = System.currentTimeMillis();
        } else {
            app.exitApp();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (text_username != null) {
            if(app.user!=null)text_username.setText("" + app.user.name);
            else text_username.setText("（登录提交分数）");
        }
    }
}
